package com.tfdy.geo;

import com.tfdy.config.CoordinateConfig;
import com.tfdy.config.PropertiesManager;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.geom.util.AffineTransformation;
import com.vividsolutions.jts.geom.util.AffineTransformationBuilder;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

/**
 * @author:LuxingZhou
 * @description:
 * @date:Created in2018/6/21  15:12
 */
public class GeoToolOperate {

    //建立工作空间
    private GeometryFactory geometryFactory = new GeometryFactory();

    private AffineTransformation atf;

    public GeoToolOperate(PropertiesManager coordinateConfig){
        //原点坐标
        Coordinate s1 = new Coordinate((Double) coordinateConfig.getProperty("startFirstX"), (Double) coordinateConfig.getProperty("startFirstY"));
        Coordinate s2 = new Coordinate((Double) coordinateConfig.getProperty("startSecondX"), (Double) coordinateConfig.getProperty("startSecondY"));
        Coordinate s3 = new Coordinate((Double) coordinateConfig.getProperty("startThirdX"), (Double) coordinateConfig.getProperty("startThirdY"));
        //目标点坐标
        Coordinate t1 = new Coordinate((Double) coordinateConfig.getProperty("toFirstX"), (Double) coordinateConfig.getProperty("toFirstY"));
        Coordinate t2 = new Coordinate((Double) coordinateConfig.getProperty("toSecondX"), (Double) coordinateConfig.getProperty("toSecondY"));
        Coordinate t3 = new Coordinate((Double) coordinateConfig.getProperty("toThirdX"), (Double) coordinateConfig.getProperty("toThirdY"));
        // 建立仿射变换对象
        AffineTransformationBuilder afb = new AffineTransformationBuilder(s1,
                s2, s3, t1, t2, t3);
        atf = afb.getTransformation();
    }


    /**
     * CAD 地方平面坐标转经纬度坐标
     * @param x
     * @param y
     * @return 经纬度点对象
     */
    public Point covertCADxyToLatLng(double x,double y){
        double [] latlng = new double[2];
        Coordinate coord = new Coordinate(x, y);
        Point pointReuslt = geometryFactory.createPoint( coord );
        // 坐标转换
        pointReuslt.apply(atf);
        return pointReuslt;
    }



    /**
     * 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中
     * @param x
     * @param y
     * @param geometry wkt格式
     * @return
     */
    public boolean withinGeo(double x,double y,String geometry) throws ParseException {
        Coordinate coord = new Coordinate(x,y);
        Point point = geometryFactory.createPoint( coord );
        WKTReader reader = new WKTReader( geometryFactory );
        if(geometry.indexOf("MULTIPOLYGON") > -1){
            //多面
            MultiPolygon multiPolygon = (MultiPolygon) reader.read(geometry);
            return point.within(multiPolygon);
        }else{
            //简单面
            Polygon polygon = (Polygon) reader.read(geometry);
            return point.within(polygon);
        }
    }

    /**
     *  根据xy级JSON面 判断点是否再面中
     * @param x 坐标x
     * @param y 坐标y
     * @param geoJsonStr  JSON
     * @return  true or false
     * @throws ParseException
     */
    public boolean withinGeometyr(double x,double y,String geoJsonStr) throws ParseException {
        geoJsonStr = geoJsonStr.replaceAll("\\]\\]\\,\\[\\[","):(");
        geoJsonStr = geoJsonStr.replaceAll("\\]\\,\\[",":");
        geoJsonStr = geoJsonStr.replaceAll("\\,"," ");
        geoJsonStr = geoJsonStr.replaceAll("\\[\\[\\[","((");
        geoJsonStr = geoJsonStr.replaceAll("\\]\\]\\]","))");
        geoJsonStr = geoJsonStr.replaceAll("\\:",",");
        if(geoJsonStr.indexOf("[") > -1){
            geoJsonStr = geoJsonStr.replaceAll("\\[","(");
            geoJsonStr = geoJsonStr.replaceAll("\\]",")");
            geoJsonStr = "MULTIPOLYGON" + geoJsonStr;
        }else{
            geoJsonStr = "POLYGON" + geoJsonStr;
        }
        boolean isIn = withinGeo(x,y,geoJsonStr);
        return isIn;
    }

}
